package com.example.api.config;

import com.common.holder.UserHolder;
import com.example.api.client.fallback.UserClientFallBack;
import feign.RequestInterceptor;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Configuration
public class DeafultFeignConfig {
    @Bean
    public RequestInterceptor requestInterceptor(){
        return requestTemplate -> {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();

            Long userId = UserHolder.getUserId();
            System.out.println("IIINNNNTTTEEERRR执行了");
            if(userId == null){
                return;
            }
            requestTemplate.header("userId",userId.toString());
            String token = request.getHeader("token");
            if (token != null) {
                requestTemplate.header("token", token);
            }
//            requestTemplate.header("token");
        };
    }
    @Bean
    public UserClientFallBack userClientFallBack(){
        return new UserClientFallBack();
    }
}
